package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.queryParams.FaultageQueryParams;
import com.css.fxfzypg.modules.baseData.repository.FaultageRepository;
import com.css.fxfzypg.modules.baseData.entity.FaultageEntity;
import com.css.fxfzypg.modules.baseData.service.FaultageService;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-08-17 14:14
 */
@Service
public class FaultageServiceImpl implements FaultageService {


    @Resource
    private FaultageRepository faultageRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FaultageEntity saveFaultage(FaultageEntity faultageEntity) {
        faultageEntity.setFauId(UUIDGenerator.getUUID());
        faultageEntity.setFauCreatetime(PlatformDateUtils.getCurrentTimestamp());
        faultageEntity.setFauIsdelete(YNEnum.Y.toString());
        return faultageRepository.saveFaultage(faultageEntity);
    }

    @Override
    public FaultageEntity findFaultageById(String dataFaultageId) {
        return faultageRepository.findFaultageById(dataFaultageId);
    }

    @Override
    public List<FaultageEntity> getFaultByTaskId(String taskId) {
        return faultageRepository.getFaultByTaskId(taskId);
    }

    @Override
    public JSONObject getAllFaultageByParams(FaultageQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(faultageRepository.getAllFaultageByParams(queryParams));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFaultage(List<String> ids) {
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        faultageRepository.deleteFaultage(ids,currentTimestamp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportExcel(HttpServletResponse response)throws IOException {
        List<FaultageEntity> list = faultageRepository.exportExcel();
        FileUtil.exportExcel(list, "断层数据", "sheet", FaultageEntity.class, "FaultageExcel", response);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<FaultageEntity> faultageEntities = FileUtil.importExcel(file, FaultageEntity.class);
        for (FaultageEntity faultageEntity : faultageEntities) {
            faultageEntity.setFauId(UUIDGenerator.getUUID());
            faultageEntity.setFauIsdelete(YNEnum.Y.toString());
            faultageEntity.setFauCreatetime(PlatformDateUtils.getCurrentTimestamp());
        }
        faultageRepository.importExcel(faultageEntities);
    }
}
